using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.CommonPage;
using System.IO;

public partial class admin_CommonPageContent : System.Web.UI.Page
{
    public bool _ReloadTop = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        LanguageUtility.CheckLanguage(Request, Response, Session);

        if (!Context.User.Identity.IsAuthenticated)
        {
            this.divMain.InnerHtml = "<a href=\"#\" onclick=\"top.location.href='login.aspx?ReturnUrl=' + encodeURIComponent(top.location.href);\">" + LanguageUtility.GetTrueLanguageText("LOGIN_REQUIRED", Session) + "</a>";
            return;
        }

        try
        {
            string control = String.Empty;
            string action = Request.QueryString["action"].Trim();

            if ("view".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonPageMenuContent.ascx";
            }
            else if ("addmnu".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonPageMenuInput.ascx";
            }
            else if ("editmnu".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonPageMenuInput.ascx";
            }
            else if ("deletemnu".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int mnuid = Int32.Parse(Request.QueryString["mnuid"].Trim());
                    this.DeleteMenu(mnuid);
                }
                catch (Exception) { }

                _ReloadTop = true;
                control = "webparts/Common/Blank.ascx";
                return;
            }
            else if ("manmenu".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonMenuManagement.ascx";
            }
            else if ("moveupmenu".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveMenuUp(Int32.Parse(Request.QueryString["mnuid"]));
                }
                catch (Exception) { }
                string returnUrl = "";
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    returnUrl = Request.QueryString["ReturnUrl"];
                }
                else
                {
                    returnUrl = "CommonPageContent.aspx?action=manmenu&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString();
                }
                Response.Redirect(returnUrl, false);
            }
            else if ("movedownmenu".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveMenuDown(Int32.Parse(Request.QueryString["mnuid"]));
                }
                catch (Exception) { }
                string returnUrl = "";
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    returnUrl = Request.QueryString["ReturnUrl"];
                }
                else
                {
                    returnUrl = "CommonPageContent.aspx?action=manmenu&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString();
                }
                Response.Redirect(returnUrl, false);
            }
            else if ("addcontent".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonPageContentInput.ascx";
            }
            else if ("editcontent".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonPageContentInput.ascx";
            }
            else if ("deletecontent".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int contentid = Int32.Parse(Request.QueryString["contentid"].Trim());
                    this.DeleteContent(contentid);
                }
                catch (Exception) { }

                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
                return;
            }
            else if ("moveupcontent".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveContentUp(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("movedowncontent".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveContentDown(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("addalbum".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonAlbumInput.ascx";
            }
            else if ("editalbum".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonAlbumInput.ascx";
            }
            else if ("delalbum".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int albumID = Int32.Parse(Request.QueryString["id"].Trim());
                    this.DeleteAlbum(albumID);
                }
                catch (Exception) { }
                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("viewalbum".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonAlbumContent.ascx";
            }
            else if (action.Equals("moveupalbum", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveAlbumDown(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if (action.Equals("movedownalbum", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveAlbumUp(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("addimage".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonImageInput.ascx";
            }
            else if ("editimage".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonImageInput.ascx";
            }
            else if (action.Equals("moveupimage", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveImageUp(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("CommonPageContent.aspx?action=viewalbum&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&id=" + Request.QueryString["albumid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if (action.Equals("movedownimage", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveImageDown(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("CommonPageContent.aspx?action=viewalbum&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&id=" + Request.QueryString["albumid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if (action.Equals("delimage", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.DeleteImage(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("CommonPageContent.aspx?action=viewalbum&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&id=" + Request.QueryString["albumid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("movemenu".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonMoveItem.ascx";
            }
            else if ("movecontent".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonMoveItem.ascx";
            }
            else if ("movealbum".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/CommonPage/CommonMoveItem.ascx";
            }
            else if ("setdefaultcontent".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int contentid = Int32.Parse(Request.QueryString["contentid"].Trim());
                    this.SetDefaultContent(contentid);
                }
                catch (Exception) { }

                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("unsetdefaultcontent".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int contentid = Int32.Parse(Request.QueryString["contentid"].Trim());
                    this.UnSetDefaultContent(contentid);
                }
                catch (Exception) { }

                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("setdefaultalbum".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int id = Int32.Parse(Request.QueryString["id"].Trim());
                    this.SetDefaultAlbum(id);
                }
                catch (Exception) { }

                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("unsetdefaultalbum".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int id = Int32.Parse(Request.QueryString["id"].Trim());
                    this.UnSetDefaultAlbum(id);
                }
                catch (Exception) { }

                Response.Redirect("CommonPageContent.aspx?action=view&g=" + Request.QueryString["g"] + "&mnuid=" + Request.QueryString["mnuid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("reloadtop".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Common/Blank.ascx";
                _ReloadTop = true;
            }

            if (control == String.Empty)
            {
                control = "webparts/Common/InvalidURL.ascx";
            }

            if (control != String.Empty)
            {
                this.divMain.Controls.Add(LoadControl(control));
            }
        }
        catch (Exception) { }
    }

    public void DeleteMenu(int menuID)
    {
        CommonMenu menu = new CommonMenu(menuID);
        if (menu.ID > 0)
        {
            menu.Delete();
        }
    }

    public void DeleteContent(int contentID)
    {
        CommonPageContent content = new CommonPageContent(contentID);
        if (content.ID > 0)
        {
            if (content.MiniImage.Trim().Length > 0)
            {
                try
                {
                    string filePath = Server.MapPath("../" + Constant.UPLOAD_FILE_PATH + "commonpage/" + content.MiniImage);
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                }
                catch { }
            }
            if (content.Image.Trim().Length > 0)
            {
                try
                {
                    string filePath = Server.MapPath("../" + Constant.UPLOAD_FILE_PATH + "commonpage/" + content.Image);
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                }
                catch { }
            }
            content.Delete();
        }
    }

    private void MoveMenuUp(int menuID)
    {
        try
        {
            CommonMenu content = new CommonMenu(menuID);
            content.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveMenuDown(int menuID)
    {
        try
        {
            CommonMenu content = new CommonMenu(menuID);
            content.MoveDown();
        }
        catch (Exception) { }
    }

    private void MoveContentUp(int contentID)
    {
        try
        {
            CommonPageContent content = new CommonPageContent(contentID);
            content.MoveDown();
        }
        catch (Exception) { }
    }

    private void MoveContentDown(int contentID)
    {
        try
        {
            CommonPageContent content = new CommonPageContent(contentID);
            content.MoveUp();
        }
        catch (Exception) { }
    }

    public void SetDefaultContent(int contentID)
    {
        CommonPageContent content = new CommonPageContent(contentID);
        if (content.ID > 0)
        {
            content.SetAtDefault();
        }
    }

    public void UnSetDefaultContent(int contentID)
    {
        CommonPageContent content = new CommonPageContent(contentID);
        if (content.ID > 0)
        {
            content.UnSetAtDefault();
        }
    }

    public void SetDefaultAlbum(int id)
    {
        CommonAlbum content = new CommonAlbum(id);
        if (content.ID > 0)
        {
            content.SetAtDefault();
        }
    }

    public void UnSetDefaultAlbum(int id)
    {
        CommonAlbum content = new CommonAlbum(id);
        if (content.ID > 0)
        {
            content.UnSetAtDefault();
        }
    }

    private void MoveAlbumUp(int albumID)
    {
        try
        {
            CommonAlbum album = new CommonAlbum(albumID);
            album.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveAlbumDown(int albumID)
    {
        try
        {
            CommonAlbum album = new CommonAlbum(albumID);
            album.MoveDown();
        }
        catch (Exception) { }
    }

    private void MoveImageUp(int imageID)
    {
        try
        {
            CommonImage detail = new CommonImage(imageID);
            detail.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveImageDown(int imageID)
    {
        try
        {
            CommonImage detail = new CommonImage(imageID);
            detail.MoveDown();
        }
        catch (Exception) { }
    }

    private void DeleteImage(int imageID)
    {
        try
        {
            CommonImage detail = new CommonImage(imageID);
            CommonAlbum album = new CommonAlbum(detail.AlbumID);

            string imageFileName = "../" + album.ImagePath + detail.ImageFileName;
            if (System.IO.File.Exists(Server.MapPath(imageFileName)))
            {
                System.IO.File.Delete(Server.MapPath(imageFileName));
            }

            string miniImageFileName = "../" + album.ImagePath + "Mini_" + detail.ImageFileName;
            if (System.IO.File.Exists(Server.MapPath(miniImageFileName)))
            {
                System.IO.File.Delete(Server.MapPath(miniImageFileName));
            }

            detail.Delete();
        }
        catch (Exception) { }
    }

    private void DeleteAlbum(int albumID)
    {
        try
        {
            CommonAlbum album = new CommonAlbum(albumID);
            if (album.ID > 0)
            {
                DataSet ds = (new CommonImage()).GetAll(album.ID);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        int imgID = (int)row[CommonImage.ID__COLUMN_NAME];
                        this.DeleteImage(imgID);
                    }
                }
            }

            string imageFileName = "../" + album.ImagePath + album.Image;
            if (System.IO.File.Exists(Server.MapPath(imageFileName)))
            {
                System.IO.File.Delete(Server.MapPath(imageFileName));
            }

            string miniImageFileName = "../" + album.ImagePath + "Mini_" + album.Image;
            if (System.IO.File.Exists(Server.MapPath(miniImageFileName)))
            {
                System.IO.File.Delete(Server.MapPath(miniImageFileName));
            }

            try
            {
                if (System.IO.Directory.Exists(Server.MapPath("../" + album.ImagePath)))
                {
                    //System.IO.Directory.Delete(Server.MapPath("../" + album.ImagePath), true);
                    this.DeleteFolder(Server.MapPath("../" + album.ImagePath));
                }
            }
            catch { }

            album.Delete();
        }
        catch (Exception)
        {
        }
    }

    private void DeleteFolder(string path)
    {
        try
        {
            string[] files = Directory.GetFiles(path);
            if (files != null)
            {
                for (int i = 0; i < files.Length; i++)
                {
                    File.Delete(files[i]);
                }
            }
            //System.IO.Directory.Delete(path);
        }
        catch { }
    }
}
